import UMe from "../../frameSccript/system/OverallSystem ";
import Ipha_tool from "../Ipha_tool";
import Ipha_ComponentBasic from "./Ipha_ComponentBasic";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Ipha_NumberLabel extends Ipha_ComponentBasic {

    comName: string = "数字显示文本增加与减少";

    @property({ type: cc.Label, displayName: "文本" })
    private lbNumber: cc.Label = null;

    @property({ displayName: "变动单位时间" })
    private unitTime: number = 1;

    @property({ displayName: "变动单位数量" })
    private unitNumber: number = 1;

    @property({ displayName: "文本前缀" })
    private str: string = "";

    @property({ displayName: "接收事件key" })
    private eventKey: string = "";

    private addedNumber: number = 0;
    private timer: number = 0;

    private showNumber: number = 0;

    onLoad() {
        UMe.Canvas().on(this.eventKey, this._event, this);
    }

    private _event(targetNumber: number) {
        this.addedNumber = targetNumber - this.showNumber;

        if (this.str != null && this.str != "") {
            this.lbNumber.string = this.str + ":" + targetNumber.toFixed(0);
        } else {
            this.lbNumber.string = targetNumber.toFixed(0);
        }
    }

    start() {

    }

    update(dt) {
        if (this.addedNumber == 0) {
            return;
        }
        // this.timer += dt;
        // if (this.timer >= this.unitTime) {
        //     this.timer = 0;
        //     let unitMul = Math.abs(this.addedNumber);
        //     unitMul = Math.ceil(Math.log10(unitMul) - 2);
        //     unitMul = Math.max(1, unitMul);
        //     unitMul = Math.pow(10, unitMul);
        //     let changeNum = Math.sign(this.addedNumber) * this.unitNumber * unitMul;
        //     if (Math.abs(this.addedNumber) < this.unitNumber * unitMul) {
        //         changeNum = this.addedNumber;
        //     }
        //     this.showNumber = Math.min(this.showNumber + this.addedNumber, this.showNumber + changeNum);
        //     this.showNumber = Math.max(this.showNumber, 0);

        //     this.addedNumber -= changeNum;

        //     if (this.str != null && this.str != "") {
        //         this.lbNumber.string = this.str + ":" + this.showNumber.toFixed(0);
        //     } else {
        //         this.lbNumber.string = this.showNumber.toFixed(0);
        //     }
        // }
    }
}
